<template>
  <div>
    <div class="content">
      <div v-show="defaultIndex==1">
        <!-- 通过手机验证 -->
        <identity-through-mobile @nextStep='nextStep(2)' v-show="queryType=='mobile'"></identity-through-mobile>
        <!-- 通过邮箱验证 -->
        <identity-through-email @nextStep='nextStep(2)' v-show="queryType=='email'"></identity-through-email>
      </div>
      <!-- 重置交易密码 -->
      <div class="form" v-show="defaultIndex==2">
        <h3 class="user-title">{{'USER_TRADE_PASS'|t}}</h3>
        <zx-form-group :title="$t('MOBILE_LABEL')">
          <zx-input v-model="passForm.phone" type="text" :place="$t('MOBILE_PLACE')" ref="phone" :rules="rules.phone">
            <span class="input-text" slot="text" @click="getCode(passForm.phone,'phone',$t('GET_CODE'),$event,'phoneCodeBool')">{{'GET_CODE'|t}}</span>
          </zx-input>
        </zx-form-group>
        <zx-form-group :title="$t('CODE_LABEL')">
          <zx-input v-model="passForm.phone_code" :place="$t('CODE_PLACE')" ref="phone_code" :rules="rules.code">
          </zx-input>
        </zx-form-group>
        <zx-form-group :title="$t('PASSWORD')">
          <zx-input v-model="passForm.pass" type="password" :place="$t('PASSWORD_ENTER')" ref="pass" :rules="rules.password">
          </zx-input>
        </zx-form-group>
        <zx-form-group :title="$t('CHECK_PASS')">
          <zx-input v-model="passForm.checkPass" type="password" :place="$t('CHECK_PASS_PLACE')" ref="checkPass" :rules="rules.password">
          </zx-input>
        </zx-form-group>
        <zx-form-group>
          <input type="submit" class="submit-btn mt-20  green-btn radius" :value="$t('COMFIRM')" @click="submitForm('passForm')">
        </zx-form-group>
      </div>
    </div>
  </div>
</template>
<script>
  import { mapGetters } from 'vuex';
  import { Form, Func, Auth, Rules } from '@/providers';
  import { User, UserInfo } from '@/services';
  import identityThroughMobile from '@/components/identity-through-mobile';
  import identityThroughEmail from '@/components/identity-through-email';
  export default {
    data() {
      return {
        queryType: 'mobile',
        defaultIndex: 1,
        phoneCodeBool: false,
        passForm: {
          phone: Auth.getItem('phone'),
          phone_code: '',
          pass: '',
          checkPass: ''
        },
        rules: Rules
      };
    },
    methods: {
      // 下一步
      async nextStep(step) {
        this.defaultIndex = step;
      },
      // 获取验证码
      getCode(val, type, innerText, event, clickBool) {
        if (this[clickBool]) { return false; };
        if (!Form.validItem(this, type)) { return; };
        this[clickBool] = true;
        this.SendMobile(val, event.target, clickBool, innerText);
      },
      // 发送手机
      async SendMobile(val, target, clickBool, innerText) {
        try {
          let res = await UserInfo.SendMobile({
            mobile: val
          });
          this.$dialog.toast({
            msg: res.desc,
            position: 'top'
          });
          Func.countdown(target, this, clickBool, innerText);
        } catch (error) {
          console.log(error);
        }
      },
      // 表单提交
      async submitForm(formName, type) {
        if (!Form.validataForm(this, formName)) {
          return false;
        }
        let _parmas = this[formName];
        if (_parmas.pass != _parmas.checkPass) {
          this.$dialog.toast({
            msg: '两次输入密码不一样',
            position: 'top'
          });
          return false;
        }
        try {
          let res = await User.SetDealPwd({
            password: Func.PasswordEncrypt(_parmas.checkPass),
            userNo: Auth.getItem('userId'),
            phoneCode: _parmas.phone_code,
            token: Auth.getItem('token')
          });
          console.log(res);
          if (!Form.checkResToken(res)) return false;
          this.$dialog.toast({
            msg: res.desc,
            position: 'top',
            onOk: () => {
              this.$store.dispatch('FIRSTLOAD');
            }
          });
        } catch (error) {
          console.log(error);
        }
      }
    },
    components: {
      'identity-through-mobile': identityThroughMobile,
      'identity-through-email': identityThroughEmail
    },
    created() {
      this.queryType = this.$route.query.type;
    }
  };
</script>
<style scoped lang="scss">
  @import "../../assets/sass/user.scss";
</style>